package servlet;

import impl.UserImpl;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletConfig;
import model.User;
import settings.Settings;
import utils.Util;

/**
 * Servlet implementation class Login
 */
public class Index extends BaseServlet {

    private UserImpl userDao;

    @Override
    public void init(ServletConfig config) throws ServletException {
        userDao = UserImpl.getInstance();
        super.init(config);
    }

    @Override
    protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //HttpSession session = request.getSession();
        //User user = (User) (session == null ? null : session.getAttribute("user"));
        int id = Util.fixInt(request.getParameter("id"));
        User user = userDao.getById(id);
        if (user != null) {
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
            return;
        } else {
            System.out.println("error: нет юзера в бд!");
        }
        request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
    }
}
